﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using patents_robot_demo.NetWorkOperate;

using patents_robot_demo.BasicWork;
namespace patents_robot_demo.Analysis
{
    class FormatInformation
    {
        #region 专利信息数据格式化
        /// <summary>
        /// 获得专利信息字符串
        /// </summary>
        /// <param name="PatentID">专利号</param>
        /// <returns>专利信息详细字符串</returns>
        static public StringBuilder RecPatnetINFO(string PatentID)
        {
            string html = GainHTML.GetPatentInfo(PatentID);
            html = html.FormatHtml();
            StringBuilder PatentINFO = new StringBuilder();
            //测试数据完整性
            int Test = html.IndexOf("摘要");
            if (Test == -1)
            {
                //log.errorlog("GET得到的数据不完整！专利号码:" + PatentID + ".");
                //return info;
            }
            //测试模块完毕
            int t1, t2;
            //获得专利号0
            t1 = html.IndexOf("申请号：");
            t2 = html.IndexOf("申请日：");
            PatentINFO.Append("PatentID=");
            PatentINFO.Append(html.Substring(t1 + 4, t2 - t1 - 4) + "\n");
            html = html.Substring(t2);
            //获得申请日1
            t1 = html.IndexOf("申请日：");
            t2 = html.IndexOf("名称：");
            PatentINFO.Append("ApplyDATE=");
            PatentINFO.Append(html.Substring(t1 + 4, t2 - t1 - 4) + "\n");
            html = html.Substring(t2);
            //获得名称2
            t1 = html.IndexOf("名称：");
            t2 = html.IndexOf("公开(公告)号：");
            PatentINFO.Append("PatentNAME=");
            PatentINFO.Append(html.Substring(t1 + 3, t2 - t1 - 3) + "\n");
            html = html.Substring(t2);
            //获得公开号3
            t1 = html.IndexOf("公开(公告)号：");
            t2 = html.IndexOf("公开(公告)日：");
            PatentINFO.Append("PublicNUM");
            PatentINFO.Append(html.Substring(t1 + 10, t2 - t1 - 8 - 2) + "\n");
            html = html.Substring(t2);
            //获得公开日4
            t1 = html.IndexOf("公开(公告)日：");
            t2 = html.IndexOf("主分类号：");
            PatentINFO.Append("PublicDATE=");
            PatentINFO.Append(html.Substring(t1 + 8, t2 - t1 - 8) + "\n");
            html = html.Substring(t2);
            //获得主分类号5
            t1 = html.IndexOf("主分类号：");
            t2 = html.IndexOf("分案原申请号：");
            PatentINFO.Append("MainSortNUM=");
            PatentINFO.Append(html.Substring(t1 + 5, t2 - t1 - 5) + "\n");
            html = html.Substring(t2);
            //获得分类号6
            t1 = html.IndexOf("分类号：");
            t2 = html.IndexOf("颁证日：");
            PatentINFO.Append("SortNUM=");
            PatentINFO.Append(html.Substring(t1 + 4, t2 - t1 - 4) + "\n");
            html = html.Substring(t2);
            //获得申请人7
            t1 = html.IndexOf("申请(专利权)人：");
            t2 = html.IndexOf("地址：");
            PatentINFO.Append("ApplyPEOPLE=");
            PatentINFO.Append(html.Substring(t1 + 9, t2 - t1 - 9) + "\n");
            html = html.Substring(t2);
            //获得地址8
            t1 = html.IndexOf("地址：");
            t2 = html.IndexOf("发明(设计)人：");
            PatentINFO.Append("Address=");
            PatentINFO.Append(html.Substring(t1 + 3, t2 - t1 - 3) + "\n");
            html = html.Substring(t2);
            //获得发明人9
            t1 = html.IndexOf("发明(设计)人：");
            t2 = html.IndexOf("国际申请：");
            PatentINFO.Append("Inventor=");
            PatentINFO.Append(html.Substring(t1 + 8, t2 - t1 - 8) + "\n");
            html = html.Substring(t2);
            //获得专利代理机构10
            t1 = html.IndexOf("专利代理机构：");
            t2 = html.IndexOf("代理人：");
            PatentINFO.Append("PatentAgencyCOMPANY=");
            PatentINFO.Append(html.Substring(t1 + 7, t2 - t1 - 7) + "\n");
            html = html.Substring(t2);
            //获得代理人11
            t1 = html.IndexOf("代理人：");
            t2 = html.IndexOf("摘要　");
            PatentINFO.Append("Agent=");
            PatentINFO.Append(html.Substring(t1 + 4, t2 - t1 - 4) + "\n");
            html = html.Substring(t2);
            //获得摘要12
            t1 = html.IndexOf("摘要　");
            PatentINFO.Append("Summary=");
            PatentINFO.Append(html.Substring(t1 + 3) + "\n");
            return PatentINFO;
        }
        #endregion
        #region 获得法律状态
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PatentId"></param>
        /// <returns></returns>
        static public string GetLawState(string LawStateHTML)
        {
            string t;
            t = "LawSTATE=授权\n" + "DisChargeTIME=0";
            LawStateHTML = LawStateHTML.FormatHtml();
            int t1, t2;
            t1 = LawStateHTML.IndexOf("专利权的终止");
            if (t1 != -1)
            {
                t1 = LawStateHTML.IndexOf("法律状态公告日");
                t2 = LawStateHTML.IndexOf("法律状态类型");
                t = "LawSTATE=终止\n" + "DisChargeTIME=" + LawStateHTML.Substring(t1 + 7, t2 - t1 - 7) + "\n";

            }
            return t;
        }
        #endregion
        #region 解析获得的专利号码HTML页 获得专利号
        /// <summary>
        /// 解析获得的专利号码HTML页 获得专利号
        /// </summary>
        /// <param name="PatentIDHTML">专利号码HTML</param>
        /// <returns>专利号码</returns>
        static public string GetPatentId(string PatentIDHTML)
        {
            string ID = null;
            int t = 0;
            int t1, t2;
            while (PatentIDHTML.IndexOf("=CN") != -1)
            {
                t1 = PatentIDHTML.IndexOf("=CN");
                t2 = PatentIDHTML.IndexOf("&leixin");
                t++;
                if (t % 2 == 1)
                {
                    PatentIDHTML = PatentIDHTML.Substring(t2 + 3);
                    continue;
                }
                ID += "ID=";
                ID += PatentIDHTML.Substring(t1 + 3, t2 - t1 - 3) + "\n";
                PatentIDHTML = PatentIDHTML.Substring(t2 + 3);
            }
            return ID;
        }
        #endregion
    }
}
